package com.gemme.gemme.wode;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.login.GlideCircleTransform;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.PayDialog;
import com.gemme.gemme.view.RoundCheckBox;
import com.gemme.gemme.view.VipSgView;
import com.jaeger.library.StatusBarUtil;

import org.json.JSONObject;

import java.util.HashMap;

//侧滑删除地址
public class Vip3Activity extends BaseActivity{
    int status = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip3);
        StatusBarUtil.setTransparent(this);
        initView();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DataManager.refreshToken(mContext);
                getData();
            }
        }).start();

    }



    VipSgView vipSgView1;
    VipSgView vipSgView2;
    public  int type = TYPE_ZHUANSHU;
    public static final int TYPE_ZHUANSHU = 1;
    public static final int TYPE_TIYAN = 2;
    TextView tv_price_real;
    TextView tv_vip_end_time;
    TextView tv_vip_name;
    ImageView iv_vip_head;
    ImageView arrow_left,arrow_left_1;
    boolean isVip = false;
    RelativeLayout rl_price;
    private void initView() {
        vipSgView1 = findViewById(R.id.vipSgView1);
        vipSgView2 = findViewById(R.id.vipSgView2);
        tv_price_real = findViewById(R.id.tv_price_real);
        vipSgView1.setPrice("288");
        vipSgView2.setPrice("28");
        vipSgView2.setName("专属会员");
        arrow_left = findViewById(R.id.arrow_left);
        arrow_left_1 = findViewById(R.id.arrow_left1);
        arrow_left_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        arrow_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        rl_price = findViewById(R.id.rl_price);
        rl_price.setVisibility(View.GONE);
        tv_vip_end_time = findViewById(R.id.tv_vip_end_time);
        tv_vip_name = findViewById(R.id.tv_vip_name);
        iv_vip_head = findViewById(R.id.iv_vip_head);
//        changeState();
        controlSelect();
        initIsVipView();

        tv_price_real.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new PayDialog().showDialog(mContext);
            }
        });
    }

    private void controlSelect() {
        RoundCheckBox cbox1 = vipSgView1.getCbox();
        RoundCheckBox cbox2 = vipSgView2.getCbox();
        cbox1.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                boolean checked = cbox1.isChecked();
                vipSgView1.setIsCheck( cbox1.isChecked());
                if(checked){
                    type = TYPE_ZHUANSHU;
                }

                changeState();
            }
        });

        cbox2.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                boolean checked = cbox2.isChecked();
                vipSgView2.setIsCheck(checked);
                if(checked){
                    type = TYPE_TIYAN;
                }
                changeState();

            }
        });

    }

    private void initIsVipView() {
        View vip_no = findViewById(R.id.vip_no);
        View vip_yes = findViewById(R.id.vip_yes);
        if(isVip){
            vip_yes.setVisibility(View.VISIBLE);
            vip_no.setVisibility(View.GONE);
        }else{
            vip_yes.setVisibility(View.GONE);
            vip_no.setVisibility(View.VISIBLE);
        }

    }

    private void changeState() {
        switch (type){
            case TYPE_ZHUANSHU:
                vipSgView2.setSatate(false);
//                new PayDialog().showDialog(mContext);
                tv_price_real.setText("288");
                break;

            case TYPE_TIYAN:
                vipSgView1.setSatate(false);
//                new PayDialog().showDialog(mContext);
                tv_price_real.setText("28");
                break;
        }
    }

    public void getData(){
        String url = DataManager.BaseUrl + "user/userapp/getuserinfo";
        HashMap<String, String> headHashMap = new HashMap<>();
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);

        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    System.out.println("");
                    int anInt = (new JSONObject(response)).getJSONObject("data").getInt("isMember");
                    String nickname = (new JSONObject(response)).getJSONObject("data").getString("nickname");
                    String headImgUrl = (new JSONObject(response)).getJSONObject("data").getString("headImgUrl");
//                    Glide.with(mContext).load(headImgUrl).into(iv_vip_head);
                    Glide.with(mContext).load(StrUtil.noNUll(headImgUrl)).transform(new GlideCircleTransform(mContext))
                            .into(iv_vip_head);
                    isVip = anInt == 1 ? true : false;
                    initIsVipView();
                    tv_vip_name.setText(nickname);
                    if (isVip){
                        getVipInfo();
                    }
                }catch (Exception e){

                }
            }
        });

    }

    private void getVipInfo() {
        String url = DataManager.BaseUrl + "user/pmember/app/list";
        HashMap<String, String> headHashMap = new HashMap<>();

        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    System.out.println("");
                    String time = (new JSONObject(response)).getJSONObject("data").getJSONArray("view").
                            getJSONObject(0).getString("endTime").substring(0, 10);
                    tv_vip_end_time.setText(time + "");
                }catch (Exception e){

                }
            }
        });
    }


}